{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d63ef22e-49ef-433b-b314-0fd9d39690f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 创建数据集\n",
    "X =  np.random.rand(100,1)\n",
    "Y = 4 + 3*X + np.random.randn(100,1)\n",
    "X_b = np.c_[np.ones((100,1)),X]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "337c298c-fe46-4dbd-93f2-c0034c05df9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建超参数\n",
    "learning_rate = 0.001\n",
    "n_iterations = 10000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c4fc781-1440-4804-ac1e-71b14168d721",
   "metadata": {},
   "source": [
    "### 1，全量梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fe69d3f1-3a1c-491a-9004-6640a44e743d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#初始化θ θ1-θn\n",
    "θ = np.random.randn(2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b6ef3da4-da0c-48b3-a57e-3907ecf95023",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 反复训练，获取最后的θ\n",
    "for _ in range(n_iterations):\n",
    "    gradients =X_b.T.dot(X_b.dot(θ) - Y)\n",
    "    θ = θ - learning_rate * gradients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9b105a8a-dea2-4baf-8027-d1ecfbabf4d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.13869162]\n",
      " [2.73690159]]\n"
     ]
    }
   ],
   "source": [
    "print(θ)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34eb921f-596e-44d3-931b-738f10ef9115",
   "metadata": {},
   "source": [
    "### 2，随机梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fbb33263-c246-4f2d-850c-8a42bd2c008c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.0829893 ]\n",
      " [-0.48303057]]\n"
     ]
    }
   ],
   "source": [
    "#初始化θ θ1-θn\n",
    "θ = np.random.randn(2,1)\n",
    "print(θ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "644d2b31-4b4a-4e7d-90d0-7392efe93627",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 反复训练，获取最后的θ\n",
    "for _ in range(n_iterations):\n",
    "    random_index = np.random.randint(100)\n",
    "    Xi = X_b[random_index:random_index+1]\n",
    "    Yi = Y[random_index:random_index+1]\n",
    "    gradients =Xi.T.dot(Xi.dot(θ) - Yi)\n",
    "    θ = θ - learning_rate * gradients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b011203d-854b-4af5-b7e2-de1064e20f68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.34229389]\n",
      " [2.39530386]]\n"
     ]
    }
   ],
   "source": [
    "print(θ)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1a4f9ab-197b-4b0f-bdfd-ed612f70ff86",
   "metadata": {},
   "source": [
    "### 3，小批量梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "faf682d1-25b1-44c8-b09c-556351f25c95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.71283634]\n",
      " [-0.65176434]]\n"
     ]
    }
   ],
   "source": [
    "#初始化θ θ1-θn\n",
    "θ = np.random.randn(2,1)\n",
    "print(θ)\n",
    "bach_size = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e33297a2-0e7a-4584-975a-c44a5c37141a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 反复训练，获取最后的θ\n",
    "for _ in range(n_iterations):\n",
    "    random_index = np.random.randint(100-bach_size)\n",
    "    X_batch = X_b[random_index:random_index+bach_size]\n",
    "    Y_batch = Y[random_index:random_index+bach_size]\n",
    "    gradients =X_batch.T.dot(X_batch.dot(θ) - Y_batch)\n",
    "    θ = θ - learning_rate * gradients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6a909cae-2fe1-4417-82c6-5ac2d9bb76a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.16246589]\n",
      " [2.65860149]]\n"
     ]
    }
   ],
   "source": [
    "print(θ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "884e8d61-7c36-4e0a-9226-400104dfb8a4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
